<template>
	<view class="container">
		<!-- 页面标题 -->
		<view class="header">
			<text class="title">欢迎来到首页</text>
		</view>

		<!-- 介绍文字 -->
		<view class="introduction">
			<text>李新是世界上最好的宝宝了！么么么么</text>
		</view>

		<!-- 轮播图 -->
		<view class="swiper-container">
			<swiper 
				class="swiper" 
				:indicator-dots="true" 
				:autoplay="true" 
				interval="3000" 
				duration="500"
				circular
			>
				<swiper-item v-for="(item, index) in banners" :key="index">
					<image :src="item" mode="aspectFill" class="banner-image"></image>
				</swiper-item>
			</swiper>
		</view>

		<!-- 应用图标列表 -->
		<view class="app-grid">
			<view 
				class="app-icon" 
				v-for="(app, index) in apps" 
				:key="index"
				@click="navigateToApp(app.path)"
			>
				<image :src="app.icon" mode="aspectFit" class="icon-img"></image>
				<text class="icon-text">{{ app.name }}</text>
			</view>
		</view>

		<!-- 背景音乐按钮 -->
		<view class="music-control" @click="toggleMusic">
			<image :src="isPlaying ? playingIcon : pausedIcon" mode="aspectFit" class="music-icon"/>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			banners: [
				'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/lunbo1.jpg',
				'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/lunbo2.jpg',
				'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/lunbo3.jpg'
			],
			apps: [
				{
					name: '智能识图',
					icon: 'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/icon/zhineng.png',
					path: 'pages/intelligent/IntelligentMap'
				},
				{
					name: '纪念墙',
					icon: 'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/icon/commemorate.png',
					path: 'pages/anniversary/index'
				},
				{
					name: '猜数字',
					icon: 'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/icon/caishuzi.png',
					path: 'pages/guess/index'
				},
			],
			musicUrl: 'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/Dehors-JORDANN.mp3',
			isPlaying: false,
			playingIcon: 'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/icon/zanting.png',
			pausedIcon: 'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/icon/bofang.png',
			backgroundAudioManager: null
		};
	},
	methods: {
		navigateToApp(path) {
			if (!path) {
				uni.showToast({ title: '暂未开放', icon: 'none' });
				return;
			}
			uni.navigateTo({
				url: '/' + path
			});
		},

		initBackgroundAudio() {
			const bgAudio = uni.getBackgroundAudioManager();
			bgAudio.title = '背景音乐';
			bgAudio.singer = 'JORDANN';
			bgAudio.coverImgUrl = 'https://lover-chengmj.oss-cn-beijing.aliyuncs.com/musicBack.jpg';
			bgAudio.src = this.musicUrl;

			// 监听播放完成
			bgAudio.onEnded(() => {
				bgAudio.play();
			});

			// 播放失败处理
			bgAudio.onError((err) => {
				console.error('播放失败:', err);
				uni.showToast({ title: '音乐播放失败', icon: 'none' });
			});

			this.backgroundAudioManager = bgAudio;
		},

		toggleMusic() {
			if (!this.backgroundAudioManager) return;

			if (this.isPlaying) {
				this.backgroundAudioManager.pause();
				this.isPlaying = false;
			} else {
				this.backgroundAudioManager.play();
				this.isPlaying = true;
			}
		}
	},
	mounted() {
		this.initBackgroundAudio();

		// 页面加载后自动播放
		this.backgroundAudioManager.play();
		this.isPlaying = true;
	},

	// 页面卸载时释放资源（可选）
	onUnload() {
		if (this.backgroundAudioManager) {
			this.backgroundAudioManager.stop();
		}
	}
};
</script>

<style lang="scss">
.container {
	background-color: #f9f9f9;
	padding: 20rpx;
	font-family: "PingFang SC", sans-serif;
	position: relative;
	width: 100%;
	box-sizing: border-box;
}

.header {
	text-align: center;
	margin-top: 40rpx;
}

.title {
	font-size: 40rpx;
	font-weight: bold;
	color: #333;
}

.introduction {
	margin: 30rpx 0;
	text-align: center;
	color: #666;
	font-size: 28rpx;
	line-height: 40rpx;
}

.swiper-container {
	width: 100%;
	height: auto;
	border-radius: 16rpx;
	overflow: hidden;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
	position: relative;
	line-height: 0;
}

swiper-item {
	display: flex;
	justify-content: center;
	align-items: center;
	box-sizing: border-box;
	padding: 0;
	margin: 0;
}

.banner-image {
	width: 100%;
	height: 100%;
	object-fit: cover;
	display: block;
}

.app-grid {
	display: grid;
	grid-template-columns: repeat(3, 1fr);
	justify-content: center; /* 水平居中 */
	gap: 40rpx;
	margin-top: 50rpx;
	width: 100%;
	padding: 0 20rpx;
	box-sizing: border-box;
}

.app-icon {
	width: 180rpx;
	height: 220rpx;
	background-color: #fff;
	border-radius: 16rpx;
	padding: 20rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	box-sizing: border-box;
	transition: transform 0.2s ease;
}

.app-icon:active {
	transform: scale(0.97);
}

.icon-img {
	width: 80rpx;
	height: 80rpx;
	margin-bottom: 15rpx;
}

.icon-text {
	font-size: 26rpx;
	color: #333;
	text-align: center;
}

/* ===== 背景音乐按钮样式 ===== */
.music-control {
	position: fixed;
	right: 30rpx;
	bottom: 100rpx;
	width: 80rpx;
	height: 80rpx;
	background-color: rgba(255, 255, 255, 0.9);
	border-radius: 50%;
	box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
	display: flex;
	align-items: center;
	justify-content: center;
	z-index: 999;
}

.music-icon {
	width: 48rpx;
	height: 48rpx;
}
</style>